package com.hopu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hopu.pojo.Mydept;
import com.hopu.pojo.Myjs;
import com.hopu.service.JsService;
import com.hopu.util.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/js")
public class JsController {

    @Autowired
    private JsService jsService;

    @RequestMapping("/findAll")
    @ResponseBody
    public ResultUtils findAll(){
        PageHelper.startPage(1,14);
        List<Myjs> all = jsService.findAll();
        PageInfo page = new PageInfo(all,10);
        return  new ResultUtils(200,"yeah",page);

    }

    @RequestMapping("/insertjs")
    @ResponseBody
    public ResultUtils insertjs(@RequestParam("jname")String jname){
        System.out.println(jname);
          jsService.insertjs(jname);
        return  new ResultUtils(200,"yeah",null);

    }
    @RequestMapping("/findByname")
    @ResponseBody
    public ResultUtils findByname(@RequestParam String jname) {

        List<Myjs> bynamejs = jsService.findBynamejs();
        int i = 1;
        for (Myjs bynamej : bynamejs) {
            System.out.println(bynamejs.size()+"                   "+ i+++"                    "+bynamej.getJname().equals(jname));
            if (bynamej.getJname().equals(jname)) {
                return new ResultUtils(200, "no", null);
            }
        }
        return new ResultUtils(20,"yeah",null);
    }
    @RequestMapping("/updatejs")
    @ResponseBody
    public ResultUtils updatejs(Myjs myjs){
        System.out.println(myjs);
        jsService.updateJs(myjs);
        return  new ResultUtils(200,"yeah",null);

    }

    @RequestMapping("/findAllbyid")
    @ResponseBody
    public ResultUtils findAllbyid(@RequestParam("jid")Integer jid ){
        System.out.println(jid);
        Myjs allbyid = jsService.findAllbyid(jid);
        return  new ResultUtils(200,"yeah",allbyid);

    }


}
